//Create Lease & Rental function
function LeaseRentalView(vWidth) {
    var self = Titanium.UI.createTableView({
        backgroundColor: 'transparent',
        separatorColor: 'transparent'
    });
    var row1 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var lblMonthly = Titanium.UI.createLabel({
        text: 'Monthly Rental',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth / 2 - 2 * 10
    });
    var txtMonthly = Ti.UI.createTextField({
        left: vWidth / 2,
        right: 10,
        hintText: '',
        textAlign: "right",
        borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
        keyboardType: Titanium.UI.KEYBOARD_DECIMAL_PAD,
        width: vWidth / 2 - 10
    });
    txtMonthly.addEventListener('change', function (e) {
        Calc();
    });
    txtMonthly.addEventListener('return', function (e) {
        txtMonthly.blur();
    });
    var row2 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var lblTerm = Titanium.UI.createLabel({
        text: 'Lease Term',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth / 2 - 2 * 10
    });
    var txtTerm = Ti.UI.createTextField({
        left: vWidth / 2,
        textAlign: "right",
        hintText: '',
        right: 10,
        borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
        keyboardType: Titanium.UI.KEYBOARD_DECIMAL_PAD,
        width: vWidth / 2 - 10
    });
    txtTerm.addEventListener('change', function (e) {
        Calc();
    });
    txtTerm.addEventListener('return', function (e) {
        txtTerm.blur();
    });
    var row3 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var lblTotal = Titanium.UI.createLabel({
        text: 'Total',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth / 2 - 2 * 10
    });
    var txtTotal = Ti.UI.createTextField({
        editable: false,
        enabled: false,
        left: vWidth / 2,
        textAlign: "right",
        hintText: '',
        right: 10,
        color: 'red',
        borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
        keyboardType: Titanium.UI.KEYBOARD_DECIMAL_PAD,
        width: vWidth / 2 - 10
    });
    var row4 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var lblPayable = Titanium.UI.createLabel({
        text: 'Stamp Duty Payable',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth - 2 * 10,
        textAlign: 'center'
    });
    var row5 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var txtPayable = Ti.UI.createTextField({
        editable: false,
        enabled: false,
        left: 10,
        textAlign: "center",
        hintText: '',
        right: 10,
        borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
        keyboardType: Titanium.UI.KEYBOARD_DECIMAL_PAD,
        width: vWidth - 2 * 10,
        color: 'red'
    });
    row1.add(lblMonthly);
    row1.add(txtMonthly);
    row2.add(lblTerm);
    row2.add(txtTerm);
    row3.add(lblTotal);
    row3.add(txtTotal);
    row4.add(lblPayable);
    row5.add(txtPayable);
    var section2 = Ti.UI.createTableViewSection({
        height: 'auto',
        headerTitle: 'Buyers Stamp Duty'
    });
    var rowFunction = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    
    // Create a Button.
    var btnSave = Ti.UI.createButton({
        title : 'Save',
        height : 'auto',
        width : 'auto',
        top : 5,
        left : 5,
        id:'btnSave'
    });
    
    // Add to the parent view.
    rowFunction.add(btnSave);
    
    // Create a Button.
    var btnEmail = Ti.UI.createButton({
        title : 'Email',
        height : 'auto',
        width : 'auto',
        top : 5,
        right : 5,
        id:'btnEmail'
    });
    
    rowFunction.add(btnEmail);
    
    
    var data = [rowFunction, row1, row2, row3, row4, row5];
    var table = Ti.UI.createTableView({
        data: data,
        top: 0,
        backgroundColor: 'transparent',
        separatorColor: 'transparent'
    });
    self.add(table);
	self.addEventListener('click', function(e) {
		if (e.source.id === 'btnSave') {
			btnSavePress();
		} else{
			if (e.source.id === 'btnEmail') {
				btnEmailPress();
			} else{
				Ti.API.info(e.source);
			};
		};
    });
    function Calc() {
        try {
            var byPass = 0;
            var monthlyRental = parseInt(txtMonthly.value);
            var leaseTerm = parseInt(txtTerm.value);
            if (isNaN(monthlyRental) || isNaN(leaseTerm) || leaseTerm === 0 || monthlyRental === 0) {
                byPass = 1;
            } else {
                var totalRentPerYear = monthlyRental * leaseTerm;
                var stampDutyValue = 0;
                if (totalRentPerYear > 1000) {
                    var yearCount = (leaseTerm % 12);
                    if (yearCount != 0) {
                        yearCount = 1 + (leaseTerm - yearCount) / 12;
                    }
                    else {
                    	yearCount = leaseTerm / 12;
                    };
                    var averageAnualRent = totalRentPerYear % yearCount;
                    if (averageAnualRent != 0) {
                        averageAnualRent = 1 + (totalRentPerYear - averageAnualRent) / yearCount;
                    }
                    else {
                    	averageAnualRent = totalRentPerYear / yearCount;
                    };
                    var multiply = 0;
                    if (leaseTerm <= 12) {
                        multiply = 1
                    } else if (leaseTerm <= 36) {
                        multiply = 2;
                    } else {
                        multiply = 4;
                    };
                    var totalDiv = averageAnualRent % 250;
                    if (totalDiv != 0) {
                        totalDiv = 1 + (averageAnualRent - totalDiv) / 250;
                    }
                    else {
                    	totalDiv = averageAnualRent / 250;
                    };
                    stampDutyValue = totalDiv * multiply;
                };
                Ti.API.info(yearCount + ' ' + totalDiv + ' ' + multiply);
                txtTotal.value = totalRentPerYear.toString();
                txtPayable.value = stampDutyValue.toString();
            };
            if (byPass === 0) {} else {
                txtTotal.value = '';
                txtPayable.value = '';
            };
        } catch (e) {
            Ti.API.info(e);
        }
    }
    
    function btnSavePress () {
      
    }
    function btnEmailPress () {
      
    }
    return self;
}
module.exports = LeaseRentalView;